package com.yzk.aigy.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yzk.aigy.common.Result;
import com.yzk.aigy.model.system.SysRole;
import com.yzk.aigy.model.vo.AssginRoleVo;
import com.yzk.aigy.model.vo.SysRoleQueryVo;
import com.yzk.aigy.service.SysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Api(tags = "角色管理")
@RestController
@RequiredArgsConstructor
@RequestMapping("/admin/system/role")
public class SysRoleController {

    private final SysRoleService sysRoleService;

    @ApiOperation("角色分页查询")
    @PreAuthorize("hasAuthority('bnt.sysRole.list')")
    @PostMapping("/page")
    public Result<IPage<SysRole>> findRoleByPageQuery(@RequestBody SysRoleQueryVo roleQueryVo)
    {
        IPage<SysRole> page = new Page<>(roleQueryVo.getPageNum(), roleQueryVo.getPageSize());
        sysRoleService.page(
                page,
                Wrappers.<SysRole>lambdaQuery()
                        .like(roleQueryVo.getRoleName() != null && !roleQueryVo.getRoleName().isEmpty(), SysRole::getRoleName, roleQueryVo.getRoleName())
                        .orderByDesc(SysRole::getId)
        );
        return Result.ok(page);
    }

    @ApiOperation("根据id查询")
    @PreAuthorize("hasAuthority('bnt.sysRole.list')")
    @GetMapping("findRoleById/{id}")
    public Result<SysRole> findRoleById(@PathVariable Long id) {
        return Result.ok(sysRoleService.getById(id));
    }

    @ApiOperation("添加角色")
    @PreAuthorize("hasAuthority('bnt.sysRole.add')")
    @PostMapping("addRole")
    public Result<Object> addRole(@RequestBody SysRole sysRole) {
        return sysRoleService.save(sysRole) ? Result.ok() : Result.fail();
    }

    @ApiOperation("修改角色")
    @PreAuthorize("hasAuthority('bnt.sysRole.update')")
    @PostMapping("updateRole")
    public Result<Object> updateRole(@RequestBody SysRole sysRole) {
        return sysRoleService.updateById(sysRole) ? Result.ok() : Result.fail();
    }

    @ApiOperation("根据ID删除角色")
    @PreAuthorize("hasAuthority('bnt.sysRole.remove')")
    @DeleteMapping("removeRoleById/{id}")
    public Result<Object> removeRoleById(@PathVariable Long id) {
        return sysRoleService.removeById(id) ? Result.ok() : Result.fail();
    }

    @ApiOperation("批量删除")
    @PreAuthorize("hasAuthority('bnt.sysRole.remove')")
    @DeleteMapping("batchRemove")
    public Result<Object> batchRemove(@RequestBody List<Long> ids) {
        return sysRoleService.removeByIds(ids) ? Result.ok() : Result.fail();
    }

    @ApiOperation(value = "根据用户获取角色数据")
    @GetMapping("/toAssign/{userId}")
    public Result<Map<String, Object>> toAssign(@PathVariable Long userId) {
        Map<String, Object> roleMap = sysRoleService.getRolesByUserId(userId);
        return Result.ok(roleMap);
    }

    @ApiOperation(value = "根据用户分配角色")
    @PostMapping("/doAssign")
    public Result<Object> doAssign(@RequestBody AssginRoleVo assginRoleVo) {
        sysRoleService.doAssign(assginRoleVo);
        return Result.ok();
    }

}
